﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditFolder : AdminPage, IEditFolderView
    {
        private EditFolderPresenter _presenter;


        public override string HelpPage
        {
            get { return "dialogs/edit-folder.aspx"; }
        }

        #region IEditFolderView Members

        public int FolderID
        {
            get { return QueryString.GetInt32ID("id"); }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditFolder.xml"; }
        }


        public void SetParentFolders(string domain, IEnumerable<Folder> folders)
        {
            ddlParentFolders.Items.Add(new ListItem(domain, "-1"));
            SetParentFolderTree(1, folders);
        }


        public int SelectedParentFolderID
        {
            get { return int.Parse(ddlParentFolders.SelectedValue); }
            set { ddlParentFolders.SelectedValue = value.ToString(); }
        }


        public string Name
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }


        public void SetCategories(IList<Category> rootCategories, IList<Category> inheritedCategories,
                                  IList<Category> categories)
        {
            csCategories.SetCategories(rootCategories, inheritedCategories, categories);
            trCategories.Visible = rootCategories.Count > 0;
        }


        public IEnumerable<Category> GetSelectedCategories()
        {
            return csCategories.GetSelectedCategories();
        }


        public void SetAllowedAdministratorGroups(IList<AdministratorGroup> availableGroups,
                                                  IList<AdministratorGroup> inheritedGroups,
                                                  IList<AdministratorGroup> groups)
        {
            gsAllowedAdministratorGroups.SetGroups(availableGroups, inheritedGroups, groups);
            trAllowedAdministratorGroups.Visible = availableGroups.Count > 0;
        }


        public IEnumerable<AdministratorGroup> GetSelectedAllowedAdministratorGroups()
        {
            return gsAllowedAdministratorGroups.GetSelectedGroups();
        }


        public void SetDeniedAdministratorGroups(IList<AdministratorGroup> availableGroups,
                                                 IList<AdministratorGroup> inheritedGroups,
                                                 IList<AdministratorGroup> groups)
        {
            gsDeniedAdministratorGroups.SetGroups(availableGroups, inheritedGroups, groups);
            trDeniedAdministratorGroups.Visible = availableGroups.Count > 0;
        }


        public IEnumerable<AdministratorGroup> GetSelectedDeniedAdministratorGroups()
        {
            return gsDeniedAdministratorGroups.GetSelectedGroups();
        }


        public void SetRequiredUserGroups(IList<UserGroup> availableGroups, IList<UserGroup> inheritedGroups,
                                          IList<UserGroup> selectedGroups, IList<UserGroup> deselectedGroups)
        {
            gsRequiredUserGroups.SetGroups(availableGroups, inheritedGroups, selectedGroups, deselectedGroups);
            trRequiredUserGroups.Visible = availableGroups.Count > 0;
        }


        public IEnumerable<UserGroup> GetSelectedRequiredUserGroups()
        {
            return gsRequiredUserGroups.GetSelectedGroups();
        }


        public IEnumerable<UserGroup> GetDeselectedUserGroups()
        {
            return gsRequiredUserGroups.GetDeselectedGroups();
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditFolderPresenter(this);
            if (Page.IsPostBack)
                return;
            _presenter.Initialize();
            HeadingIcon = litIcon.Text;
            txtName.Focus();
        }


        protected void ddlParentFolders_IndexChanged(object sender, EventArgs e)
        {
            _presenter.UpdateCategoriesAndGroups();
        }


        protected void lbSaveFolder_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
                Response.Redirect("Explorer.aspx");
        }

        private void SetParentFolderTree(int level, IEnumerable<Folder> folders)
        {
            foreach (Folder f in folders)
            {
                string name = f.Name;
                for (int i = 0; i < level; i++)
                    name = "&nbsp;&nbsp;&nbsp;&nbsp;" + name;
                ddlParentFolders.Items.Add(new ListItem(name, f.ID.ToString()));
                SetParentFolderTree(level + 1, f.SubFolders);
            }
        }
    }
}